package com.kcl.dfss;

import java.util.Queue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

import android.app.Service;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class LocationService extends Service {
	public static final String TAG = "LocationService";
	private LocationManager mLocationManager = null;
	private LocationProvider mProvider = null;
	private LocationListener mListener = null;
	private Location mLastLocation = null;
	private Criteria mCriteria = null;
	private static Location mLatestLocation = null;
	private static float mDistance = 0.0f;
	private BlockingDeque<Location> mLocationQueue = null;
	private boolean startWork = false;
	private static LocationService mInstance = null;
	private Thread mWorkerThread = new Thread() {
		public void run() {
			float[] results = new float[1];
			while (startWork) {
				Location location;
				try {
					location = mLocationQueue.take();
					if (mLastLocation != null) {
						Location.distanceBetween(mLastLocation.getLatitude(),
								mLastLocation.getLongitude(),
								location.getLatitude(), location.getLongitude(),
								results);
						mDistance += results[1];
					} else {
						mLastLocation = location;
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
	};
	public static float getDistance(){
		return mDistance;
	}
	public static Location getLatestLocation(){
		return mLatestLocation;
	}
	public void stopCaculateDistance(){
		startWork = false;
		try {
			mWorkerThread.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mLocationManager.removeUpdates(mListener);
		mListener = null;
	}
	public void startCaculateDistance(){
		startWork = true;
		mListener = new LocationListener() {

			@Override
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				try {
					mLatestLocation = location;
					mLocationQueue.put(location);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub

			}

		};
		mLocationManager.requestLocationUpdates(1000, 5, new Criteria(),
				mListener, getMainLooper());
		
		mWorkerThread.start();
	}

	public void onCreate() {
		super.onCreate();
		Log.d(TAG,"onCreate");
		mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		mCriteria = new Criteria();
		mCriteria.setAccuracy(Criteria.ACCURACY_FINE);
		mLocationQueue = new LinkedBlockingDeque<Location>();
		mInstance = this;
		
	}
	public static LocationService getInstance(){
		return mInstance;
	}
	public void onDestroy(){
		super.onDestroy();
		mInstance = null;
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}
